package com.icss.ah.delivery.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.delivery.bean.DeliveryInfoBean;
import com.icss.j2ee.util.UUID;

@Service
@Transactional
public class DeliveryAddressInfoService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	/**
	 * 查询登录人员对应店铺的收获地址信息
	 * @param map
	 * @return
	 */
	public List getDeliveryList(HashMap map) {
		List list = new ArrayList();
		try{
			list = sqlSessionTemplate.selectList("DeliveryAddressInfoBean.queryDeliveryList", map);
			infolog.info("DeliveryAddressInfoService|getDeliveryList|list:"+list);
			list = list == null ? new ArrayList() : list;
		} catch (Exception e) {
			e.printStackTrace();
			list = new ArrayList();
			errlog.error("DeliveryAddressInfoService|getDeliveryList", e);
		}
		return list;
		
	}
	
	/**
	 * 查询登录人员对应店铺的收获地址总数
	 * @param map
	 * @return
	 */
	public Long getDeliveryCount(HashMap map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne("DeliveryAddressInfoBean.getDeliveryCount", map);
			infolog.info("DeliveryAddressInfoService|getDeliveryCount|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DeliveryAddressInfoService|getDeliveryCount", e);
		} 
		return reValue;
	}
	/**
	 * 保存地址
	 * @param deliveryInfo
	 * @param addEditFlag
	 * @param uuid
	 * @return
	 */
	public boolean saveAddress( DeliveryInfoBean deliveryInfo,String addEditFlag,String uuid){
		boolean reValue = false;
		try {
			if(addEditFlag.equals("add")){
				if(uuid.equals("")){
					uuid = new UUID().toString();
				}
				deliveryInfo.setDeliveryid(uuid);
				sqlSessionTemplate.insert("DeliveryAddressInfoBean.insertAddress", deliveryInfo);
			} else {
				deliveryInfo.setDeliveryid(uuid);
				sqlSessionTemplate.update("DeliveryAddressInfoBean.updateAddress", deliveryInfo);
			}
			reValue = true;
			infolog.info("DeliveryAddressInfoService|saveAddress|reValue:"+reValue);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DeliveryAddressInfoService|saveAddress", e);
		} 
		return reValue;
	}
	
	public HashMap queryTheAddressMap(HashMap map) {
		HashMap addressMap=new HashMap();
		try {
			addressMap = (HashMap)sqlSessionTemplate.selectOne("DeliveryAddressInfoBean.queryTheAddressMap", map);
			infolog.info("DeliveryAddressInfoService|queryTheAddressMap|addressMap:"+addressMap);
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DeliveryAddressInfoService|queryTheAddressMap", e);
		} 
		return addressMap;
	}
	
	

	public boolean delAddress(String deliveryid){
		boolean reValue = false;
		try {
			int f = sqlSessionTemplate.delete("DeliveryAddressInfoBean.delAddress", deliveryid);
			infolog.info("DeliveryAddressInfoService|delAddress|f:"+f);
			reValue = true;
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("DeliveryAddressInfoService|delAddress", e);
		} 
		return reValue;
		
	}
	
	
	public List queryTheCountryList(HashMap map) {
		List list = new ArrayList();
		try{
			list = sqlSessionTemplate.selectList("DeliveryAddressInfoBean.queryTheCountryList", map);
			infolog.info("DeliveryAddressInfoService|queryTheCountryList|list:"+list);
			list = list == null ? new ArrayList() : list;
		} catch (Exception e) {
			e.printStackTrace();
			list = new ArrayList();
			errlog.error("DeliveryAddressInfoService|queryTheCountryList", e);
		}
		return list;
		
	}
}
